<template>
  <div class="app-container">
    <div class="table-page-search-wrapper">
      <el-form :inline="true" label-width="100px">
        <el-row :gutter="18">
          <el-col :md="6" :sm="24">
            <el-form-item label="任务编号">
              <el-input v-model="queryParam.taskNum" placeholder="" clearable @keyup.enter.native="onSearch" @clear="onSearch" />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="单据编号">
              <el-input v-model="queryParam.receiptNum" placeholder="" clearable @keyup.enter.native="onSearch" @clear="onSearch" />
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :md="6" :sm="24">
              <el-form-item label="料品料号">
                <el-input v-model="queryParam.productNum" placeholder="" clearable @keyup.enter.native="onSearch" @clear="onSearch" />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="24">
              <el-form-item label="备料料号">
                <el-input v-model="queryParam.stockNum" placeholder="" clearable @keyup.enter.native="onSearch" @clear="onSearch" />
              </el-form-item>
            </el-col>
          </template>
          <el-col :md="!advanced && 6 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <el-button v-waves type="primary" icon="el-icon-search" @click="onSearch">
                查询
              </el-button>
              <el-button type="info" icon="el-icon-refresh" @click="reset">
                重置
              </el-button>
              <el-link type="primary" :underline="false" @click="toggleAdvanced">
                {{ advanced ? '收起' : '展开' }}
                <i :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" />
              </el-link>
            </span>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="table-operator">
      <el-button v-permission="['create']" type="primary" icon="el-icon-plus" @click="$refs.form.open(undefined)">
        新建
      </el-button>
      <el-button v-permission="['import']" type="default" icon="el-icon-upload2" @click="handelImport">
        导入
      </el-button>
      <input
        v-show="false"
        id="fileTemp"
        ref="fileTemp"
        type="file"
        accept=".xls, .xlsx, .excel"
        @input="getImportFile"
      >
    </div>

    <el-table
      v-loading="loadingData"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
      row-key="id"
      @sort-change="appendSorterParam"
      @row-click="singleElection"
    >
      <el-table-column label="" width="65">
        <template slot-scope="{$index}">
          <el-radio v-model="templateSelection" class="radio" :label="$index">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column align="center" label="任务编号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.taskNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="单据编号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.receiptNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="计划号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.planNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="料品料号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.productNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="料品品名">
        <template slot-scope="scope">
          <span>
            {{ scope.row.productName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="料品规格">
        <template slot-scope="scope">
          <span>
            {{ scope.row.productSpecification }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备料料号">
        <template slot-scope="scope">
          <span>
            {{ scope.row.stockNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备料料品名称">
        <template slot-scope="scope">
          <span>
            {{ scope.row.stockName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备料料品规格">
        <template slot-scope="scope">
          <span>
            {{ scope.row.stockSpecification }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备料标准用量">
        <template slot-scope="scope">
          <span>
            {{ scope.row.stockDosage }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="生产数量">
        <template slot-scope="scope">
          <span>
            {{ scope.row.quantity }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="计划开工日期">
        <template slot-scope="scope">
          <span>
            {{ scope.row.startTime }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="计划完工日期">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pagination.total>0"
      :total="pagination.total"
      :page.sync="pagination.current"
      :limit.sync="pagination.pageSize"
      :style="{textAlign: 'right'}"
      @pagination="handlePaginationChanged"
    />

    <diboot-form ref="form" @complete="getList" />
    <!-- 二维码 -->
    <qrCode
      ref="qrCode"
      :label="qrModel.label"
      :text="qrModel.text"
      :qr-size="320"
    />
  </div>
</template>

<script>
import list from '@/components/diboot/mixins/list'
import dibootForm from '@/views/productmanage/productTask/form'
import waves from '@/directive/waves'
import { uploadProduct } from '@/api/product'
import { columns } from '@/views/productmanage/productTask/components/tableConfig'
import qrCode from '@/components/qrCode/index'
import { CodeNo } from '@/unit/QRCodeConfig'

export default {
  name: 'ProductTaskModal',
  directives: { waves },
  components: {
    dibootForm,
    qrCode
  },
  mixins: [list],
  data() {
    return {
      baseApi: '/productTask',
      getListFromMixin: true,
      columns,
      qrModel: {
        label: '', // 二维码底部内容
        text: '' // 二维码内容
      },
      CodeNo,
      selectedRowInfo: null,
      templateSelection: ''
    }
  },
  created() {
  },
  methods: {
    singleElection(row) {
      this.templateSelection = this.list.indexOf(row)
      this.selectedRowInfo = row
    },
    // 导入接口
    async setImport(list) {
      const excelData = JSON.stringify(list)
      const res = await uploadProduct({ excelData })
      if (res.code && res.code !== 200) {
        this.$message.error(res.msg)
        return
      } else {
        this.$message.success(res.data)
      }
      this.getList()
    },
    // 生成二维码
    handleCode(record) {
      // debugger
      const { taskNum, receiptNum, planNum, quantity, startTime, endTime, id, productNum, productName } = record
      const newObj = {
        taskNum,
        receiptNum,
        planNum,
        quantity,
        startTime,
        endTime,
        id,
        productNum,
        productName,
        QRCode: CodeNo.ProductTask
      }
      const text = JSON.stringify(newObj)
      this.$refs.qrCode.title = '任务单二维码'
      this.qrModel.label = `${receiptNum} ${taskNum}`
      this.qrModel.text = text
      this.$refs.qrCode.list = this.qrModel.label.split(',')
      this.$refs.qrCode.showModal()
    },
    showDrawer(record, refName) {
      this.$refs[refName].showDrawer(record)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
